import Taro from '@tarojs/taro'

const config = {
  baseURL: 'http://127.0.0.1:7021',
}

const request = (
  url: string,
  method: 'GET' | 'POST' | 'PUT' | 'DELETE',
  data?: string | object | ArrayBuffer
): Promise<any> => {
  return new Promise((resolve, reject) => {
    Taro.request({
      url: config.baseURL + url,
      method,
      data,
      header: {
        Authorization: Taro.getStorageSync('token') || '',
      },
      success: (res: any) => {
        resolve(res.data)
        // reject(res)
      },
      fail: (err: any) => {
        reject(err)
        Taro.showToast({
          title: '服务器发生未知错误',
          icon: 'none',
          duration: 1000,
        })
      },
    })
  })
}

export { request }
